package com.bailong.nanny.service.impl;

import com.bailong.nanny.d.UserDao;
import com.bailong.nanny.dao.impl.UserDaoImpl;
import com.bailong.nanny.entity.Page;
import com.bailong.nanny.entity.User;
import com.bailong.nanny.service.UserService;
import com.bailong.nanny.util.JdbcUtils;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    /**
     *查询全部用户
     * @return
     */
    @Override
    public List<User> selectAllUser() {
        return userDao.selectAllUser();
    }

    /**
     * 删除指定用户
     * @param id
     * @return
     */
    @Override
    public int deleteUser(int id) {
        return userDao.deleteUser(id);
    }

    /**
     * 查询全部页数
     * @param currentPage
     * @return
     */
    @Override
    public Page<User> selectUsersPage(int currentPage) {
        //查询总行数
        int count = userDao.selectUserCount();
        //计算总页数,书否除尽，否则+1
        int pageCount = count%Page.PAGE_SIZE== 0 ? count/Page.PAGE_SIZE : (count/Page.PAGE_SIZE)+1;
        System.out.println("当前页数" + pageCount);
        //把当前页数执行分页查询
        List<User>data = userDao.selectUserLimit((currentPage-1)*Page.PAGE_SIZE,Page.PAGE_SIZE);
        //把总页数，当前页数，数据集合包装到page对象中
        Page<User>page = new Page<User>();
        page.setList(data);
        page.setCurrentPage(currentPage);
        page.setPageCount(pageCount);
        //返回page对象
        return page;
    }

    /**
     * 通过ID查询用户
     * @param id
     * @return
     */
    @Override
    public User selectUserById(int id) {
        return userDao.selectUserById(id);
    }

    /**
     * 通过用户名查询用户
     * @param name
     * @return
     */
    @Override
    public User selectUserByName(String name) {
        return JdbcUtils.queryOne(User.class,"select * from tb_user where name=?",name);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    /**
     *  查询用户总数
     * @return
     */
    @Override
    public int selectUserCount() {
        return 0;
    }

    /**
     * 分页查询用户
     * @param start
     * @param length
     * @return
     */
    @Override
    public List<User> selectUserLimit(int start, int length) {
        return null;
    }


}
